package com.aliyun.servlet;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.bean.QueryPara;
import com.aliyun.service.YuMingService;
import com.aliyun.util.GetUUID;


@Controller 
public class YuMingController {
	private final Log log = LogFactory.getLog(YuMingController.class);
    
    @Resource
	private YuMingService yumingService;
    
    @ResponseBody
	@RequestMapping(value="/insertYuMing",produces = {"application/text;charset=UTF-8"}) 
	public String insertYuMing(@RequestBody String param)
	{
    	System.out.println(param);
    	String returnStr = "0";
    	String[] temps = URLDecoder.decode(param).split("&");
    	Map<String,String> map = new HashMap<String,String>();
		map.put("id", GetUUID.getId());
		//appkey:FFFF00000000017ABA4E
		//csessionid
		//sig
		//token
		//scene
		
    	for(String temp : temps){
    		if(temp.startsWith("name=")){
				map.put("name", temp.split("=")[1]);
			}else if(temp.startsWith("address=")){
				map.put("address", temp.split("=")[1]);
			}else if(temp.startsWith("username=")){
				map.put("username", temp.split("=")[1]);
			}
		}
    	try {
    		int b = yumingService.insertYuMing(map);
    		if(b>0)returnStr="1";
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return returnStr;
	}
    
    @ResponseBody
   	@RequestMapping(value="/insertYuMingList",produces = {"application/text;charset=UTF-8"}) 
   	public String insertYuMingList(@RequestBody String param)
   	{
//       	/csessionid=0152JIZgtMjy7iQLwB8JakWa3bp1c2uqSBk9r8O4Oqr4cNV5XSujoAZyYlBRGD4ad7rviIfv7UrtRa2G3hUe6Pzpo8aJrN6XvZNw8JYaFpkQIuyVx4Y2_v6Gyts4H9MjZvNBLfBX1ZL2WgKqrQIJiippa8avS4V45v-iLM86kCuE0&value=bak_default&sig=05a1C7nT4bR5hcbZlAujcdyXquG0EzLazJTxThdXHob4Cwz_CHC7mVXTNOl3fRxLfX3MDf7SErc0Ol3nyA3Yr7TkD859_IkXwcN76CpW_z0RhiGp4-RSlo2wwzH2kqN58cPE1cnR8ko6Ya6kq3DpwjIan2Cpab5LhGWMt3cUu-n8NuBj2qWq8X3gPZ1JO6txkw4TRNhbic18Iv1Lrj4dqz5X9q0_mxmwrotqFLdUEOlTmSYBZP35Mt12qjmuzdtl4QmrTnbQhsy6FjnZa0opwLtfcpWN6wmZdwiozsKzhzTF7nZkyUnUsbTtSjQeb7qCzERDcUVgc2-OhUmfwMOXXyLPRzpF8tFdj7jcDocMRv4DvjRsJXpXWcgdu9IeFp0J8cgiN3YvSTzEGJbJJClhxm49mnngZPdFJYiRP7_cT117o&token=FFFF00000000016A8646%3A1512881720983%3A0.8477336248216705
       	System.out.println(param);
       	param = param.replace("=", "");
       	List<Map<String,String>> list = new ArrayList<Map<String,String>>();
       	String returnStr = "0";
       	String[] temps = param.split("&");
     
       	for(String temp : temps){
       		//再分割
       	  	Map<String,String> map = new HashMap<String,String>();
       		map.put("id", GetUUID.getId());
       		String[] ips = temp.split("----");
       		map.put("name", ips[0]);
       		map.put("address", ips[1]);
       		map.put("username", ips[2]);
       		list.add(map);
   		}
       	try {
       		int b = yumingService.insertYuMingList(list);
       		if(b>0)returnStr="1";
   		} catch (SQLException e) {
   			e.printStackTrace();
   		}
   		return returnStr;
   	}
    @ResponseBody
   	@RequestMapping(value="/deleteYuMing",produces = {"application/text;charset=UTF-8"}) 
   	public String deleteYuMing(@RequestBody String param)
   	{
       	System.out.println(param);
       	List<Map<String,String>> list = new ArrayList<Map<String,String>>();
       	String returnStr = "0";
       	String[] temps = URLDecoder.decode(param).split(",");
     
       	for(String temp : temps){
       		//再分割
       	  	Map<String,String> map = new HashMap<String,String>();
       		map.put("name", temp);
       		list.add(map);
   		}
       	try {
       		int b = yumingService.deleteYuMing(list);
       		if(b>0)returnStr="1";
   		} catch (SQLException e) {
   			e.printStackTrace();
   		}
   		return returnStr;
   	}
    @ResponseBody
	@RequestMapping(value="/getYuMingItem",method = RequestMethod.GET,produces = {"application/text;charset=UTF-8"}) 
	public String getYuMingItem(HttpServletRequest request)
	{	
    	String username=request.getParameter("username");
    	String name=request.getParameter("name");
    	String areacode = request.getParameter("areacode");
    	String areanew = "";
    	if(areacode!=null && !"".equals(areacode)){
    		String[] temps = areacode.split(",");
    		for(String temp : temps){
    			areanew +=temp+"','";
    		}
    	}
    	
    	QueryPara qp = new QueryPara();
    	qp.setUsername(username);
    	qp.setName(name);
    	qp.setAreacode(areanew);
    	String returnStr = "";
    	try {
			List<Map<String,Object>> list = yumingService.getYuMingItem(qp);
			System.out.println(JSON.toJSONString(list));
			returnStr=JSON.toJSONString(list);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnStr;
	}
    @ResponseBody
	@RequestMapping(value="/getYuMingList",method = RequestMethod.GET,produces = {"application/text;charset=UTF-8"}) 
	public String getYuMingList(HttpServletRequest request)
	{	
    	String areacode=request.getParameter("areacode");
    	Map<String,String> map = new HashMap<String,String>();
    	map.put("areacode", areacode);
    	String returnStr = "";
    	try {
			List<Map<String,Object>> list = yumingService.getYuMingList(map);
			System.out.println(JSON.toJSONString(list));
			returnStr=JSON.toJSONString(list);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnStr;
	}
    @ResponseBody
   	@RequestMapping(value="/updateYuMingList",produces = {"application/text;charset=UTF-8"}) 
   	public void updateYuMingList(@RequestBody String param)
   	{
       	System.out.println(param);
       	//将param装换成json，然后转换成list，送入后台
       	try {
			param = URLDecoder.decode(param,"utf-8");
		} catch (UnsupportedEncodingException e1) {
		}
       	param = param.replace("returnStr=", "");
        JSONArray jArray =	JSONArray.parseArray(param);
        for(int i=0;i<jArray.size();i++){
        	Map<String,String> map = new HashMap<String,String>();
        	JSONObject yumingJ = (JSONObject)jArray.get(i);
        	map.put("name", yumingJ.get("name")+"");
        	map.put("areacode", yumingJ.get("areacode")+"");
        	map.put("currentAddress", yumingJ.get("currentaddress")+"");
        	map.put("status", yumingJ.get("status")+"");
        	try {
				yumingService.updateYuMingStatus(map);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
   	}
    @ResponseBody
	@RequestMapping(value="/getYuMingTimeList",method = RequestMethod.GET,produces = {"application/text;charset=UTF-8"}) 
	public String getYuMingTimeList(HttpServletRequest request)
	{	
    	String areacode=request.getParameter("areacode");
    	Map<String,String> map = new HashMap<String,String>();
    	map.put("areacode", areacode);
    	String returnStr = "";
    	try {
			List<Map<String,Object>> list = yumingService.getYuMingTimeList(map);
			System.out.println(JSON.toJSONString(list));
			returnStr=JSON.toJSONString(list);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnStr;
	}
    @ResponseBody
   	@RequestMapping(value="/updateYuMingTimeList",produces = {"application/text;charset=UTF-8"}) 
   	public void updateYuMingTimeList(@RequestBody String param)
   	{
       	System.out.println(param);
       	//将param装换成json，然后转换成list，送入后台
       	try {
			param = URLDecoder.decode(param,"utf-8");
		} catch (UnsupportedEncodingException e1) {
		}
       	param = param.replace("returnStr=", "");
        JSONArray jArray =	JSONArray.parseArray(param);
        for(int i=0;i<jArray.size();i++){
        	Map<String,String> map = new HashMap<String,String>();
        	JSONObject yumingJ = (JSONObject)jArray.get(i);
        	map.put("name", yumingJ.get("name")+"");
        	map.put("areacode", yumingJ.get("areacode")+"");
        	map.put("currentAddress", yumingJ.get("currentaddress")+"");
        	map.put("status", yumingJ.get("status")+"");
        	try {
				yumingService.updateYuMingStatus(map);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
   	}
}
